/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import View.Model.*;
import javax.swing.JTree;
import org.json.JSONObject;


public class ComponentPage extends javax.swing.JFrame {
    //Component component;
    JTree loArvore; 
    Project project;
    Component component;
    Porta port = new Porta();
    int componenttype;
    
    /**
     * Creates new form ComponentPage
     */
    public ComponentPage(JTree loArvore, Project project) {
        //this.component = component;
        this.loArvore = loArvore;
        this.project = project;
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btgComponent = new javax.swing.ButtonGroup();
        labelPropriedadesComponenteSelecionado1 = new javax.swing.JLabel();
        labelNomeComponenteSelecionado = new javax.swing.JLabel();
        panelComponentesDisponiveis = new javax.swing.JPanel();
        labelComponentes = new javax.swing.JLabel();
        buttonLed = new javax.swing.JRadioButton();
        buttonBuzzer = new javax.swing.JRadioButton();
        buttonSolarSensor = new javax.swing.JRadioButton();
        buttomTempSensor = new javax.swing.JRadioButton();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jLabel1 = new javax.swing.JLabel();
        txtComponentName = new javax.swing.JTextField();
        pnlBuzzer = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        txtBuzzerVolume = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtBuzzerTone = new javax.swing.JTextField();
        pnlSensor = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox<>();
        btCancel = new javax.swing.JButton();
        btOk = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        labelPropriedadesComponenteSelecionado1.setFont(new java.awt.Font("Segoe UI", 0, 14)); // NOI18N
        labelPropriedadesComponenteSelecionado1.setText("Propriedades:");

        labelNomeComponenteSelecionado.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N
        labelNomeComponenteSelecionado.setText("Component name:");

        panelComponentesDisponiveis.setBackground(new java.awt.Color(255, 255, 255));
        panelComponentesDisponiveis.setBorder(javax.swing.BorderFactory.createCompoundBorder());

        labelComponentes.setText("Componentes");

        btgComponent.add(buttonLed);
        buttonLed.setText("LED");
        buttonLed.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonLedActionPerformed(evt);
            }
        });

        btgComponent.add(buttonBuzzer);
        buttonBuzzer.setText("Buzzer");
        buttonBuzzer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonBuzzerActionPerformed(evt);
            }
        });

        btgComponent.add(buttonSolarSensor);
        buttonSolarSensor.setText("Sensor");
        buttonSolarSensor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonSolarSensorActionPerformed(evt);
            }
        });

        btgComponent.add(buttomTempSensor);
        buttomTempSensor.setText("Timer");
        buttomTempSensor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttomTempSensorActionPerformed(evt);
            }
        });

        btgComponent.add(jRadioButton1);
        jRadioButton1.setText("Button");
        jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton1ActionPerformed(evt);
            }
        });

        btgComponent.add(jRadioButton2);
        jRadioButton2.setText("Engine");
        jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelComponentesDisponiveisLayout = new javax.swing.GroupLayout(panelComponentesDisponiveis);
        panelComponentesDisponiveis.setLayout(panelComponentesDisponiveisLayout);
        panelComponentesDisponiveisLayout.setHorizontalGroup(
            panelComponentesDisponiveisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelComponentesDisponiveisLayout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addComponent(labelComponentes, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(25, 25, 25))
            .addGroup(panelComponentesDisponiveisLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelComponentesDisponiveisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(buttonLed, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonBuzzer, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonSolarSensor, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttomTempSensor, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jRadioButton1)
                    .addComponent(jRadioButton2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        panelComponentesDisponiveisLayout.setVerticalGroup(
            panelComponentesDisponiveisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelComponentesDisponiveisLayout.createSequentialGroup()
                .addGap(17, 17, 17)
                .addComponent(labelComponentes)
                .addGap(18, 18, 18)
                .addComponent(buttonLed)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonBuzzer)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonSolarSensor)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttomTempSensor)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jRadioButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRadioButton2)
                .addContainerGap(44, Short.MAX_VALUE))
        );

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel1.setText("New Component");

        jLabel2.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N
        jLabel2.setText("Volume:");

        jLabel3.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N
        jLabel3.setText("Tone:");

        javax.swing.GroupLayout pnlBuzzerLayout = new javax.swing.GroupLayout(pnlBuzzer);
        pnlBuzzer.setLayout(pnlBuzzerLayout);
        pnlBuzzerLayout.setHorizontalGroup(
            pnlBuzzerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlBuzzerLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlBuzzerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlBuzzerLayout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtBuzzerVolume, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pnlBuzzerLayout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(18, 18, 18)
                        .addComponent(txtBuzzerTone, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pnlBuzzerLayout.setVerticalGroup(
            pnlBuzzerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlBuzzerLayout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(pnlBuzzerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtBuzzerVolume, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlBuzzerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtBuzzerTone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel4.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N
        jLabel4.setText("Volume:");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Temperature", "Moisture", "Light", "Distance" }));

        btCancel.setText("Cancel");
        btCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelActionPerformed(evt);
            }
        });

        btOk.setText("OK");
        btOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btOkActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlSensorLayout = new javax.swing.GroupLayout(pnlSensor);
        pnlSensor.setLayout(pnlSensorLayout);
        pnlSensorLayout.setHorizontalGroup(
            pnlSensorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlSensorLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlSensorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlSensorLayout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlSensorLayout.createSequentialGroup()
                        .addComponent(btCancel)
                        .addGap(18, 18, 18)
                        .addComponent(btOk)))
                .addContainerGap(29, Short.MAX_VALUE))
        );
        pnlSensorLayout.setVerticalGroup(
            pnlSensorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlSensorLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlSensorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 42, Short.MAX_VALUE)
                .addGroup(pnlSensorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btCancel)
                    .addComponent(btOk)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelComponentesDisponiveis, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(labelNomeComponenteSelecionado)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtComponentName))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelPropriedadesComponenteSelecionado1)
                            .addComponent(jLabel1))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(pnlBuzzer, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(pnlSensor, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(panelComponentesDisponiveis, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(labelNomeComponenteSelecionado)
                            .addComponent(txtComponentName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(labelPropriedadesComponenteSelecionado1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(pnlBuzzer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(pnlSensor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buttonSolarSensorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSolarSensorActionPerformed
        // TODO add your handling code here:
        pnlBuzzer.setVisible(false);
        pnlSensor.setVisible(true);
        componenttype = 2;
    }//GEN-LAST:event_buttonSolarSensorActionPerformed

    private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton1ActionPerformed
        // TODO add your handling code here:
        componenttype = 4;
    }//GEN-LAST:event_jRadioButton1ActionPerformed

    private void buttonBuzzerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonBuzzerActionPerformed
        // TODO add your handling code here:
        pnlBuzzer.setVisible(true);
        pnlSensor.setVisible(false);
        componenttype = 1;
    }//GEN-LAST:event_buttonBuzzerActionPerformed

    private void btCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelActionPerformed
        // TODO add your handling code here:
        super.dispose();
    }//GEN-LAST:event_btCancelActionPerformed

    private void btOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btOkActionPerformed
        DefaultTreeModel treeModel = (DefaultTreeModel)loArvore.getModel();
        DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) loArvore.getLastSelectedPathComponent();
        DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(TipoObjeto.COMPONENT.getDescription() + this.txtComponentName.getText());
        treeModel.insertNodeInto(newNode, selectedNode, selectedNode.getChildCount());
        component = new Component(this.port,this.txtComponentName.getText(),false);
        this.project.getThing().get(0).addComponente(component);;
        this.setVisible(false);    
    }//GEN-LAST:event_btOkActionPerformed

    private void buttonLedActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonLedActionPerformed
        // TODO add your handling code here:
        componenttype = 0; //led
    }//GEN-LAST:event_buttonLedActionPerformed

    private void buttomTempSensorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttomTempSensorActionPerformed
        // TODO add your handling code here:
        componenttype = 3;
    }//GEN-LAST:event_buttomTempSensorActionPerformed

    private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton2ActionPerformed
        // TODO add your handling code here:
        componenttype = 5;
    }//GEN-LAST:event_jRadioButton2ActionPerformed

    /**
     * @param args the command line arguments
     */
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btCancel;
    private javax.swing.JButton btOk;
    private javax.swing.ButtonGroup btgComponent;
    private javax.swing.JRadioButton buttomTempSensor;
    private javax.swing.JRadioButton buttonBuzzer;
    private javax.swing.JRadioButton buttonLed;
    private javax.swing.JRadioButton buttonSolarSensor;
    private javax.swing.JComboBox<String> jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JLabel labelComponentes;
    private javax.swing.JLabel labelNomeComponenteSelecionado;
    private javax.swing.JLabel labelPropriedadesComponenteSelecionado1;
    private javax.swing.JPanel panelComponentesDisponiveis;
    private javax.swing.JPanel pnlBuzzer;
    private javax.swing.JPanel pnlSensor;
    private javax.swing.JTextField txtBuzzerTone;
    private javax.swing.JTextField txtBuzzerVolume;
    private javax.swing.JTextField txtComponentName;
    // End of variables declaration//GEN-END:variables
}
